﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using STLGROUP.Layers.Base;
using STLGROUP.Layers.DataAccessLayers;
using STLGROUP.Layers.Common;

namespace STLGROUP.Layers.BLL
{
    public class DT_SanPhamBLL : BaseBLL
    {
        public string GetNewMaSanPham(int IDKhachHang)
        {
            //IDNhaCungCap + IDKho : 0100 
            return "0100" + IDKhachHang.ToString().PadLeft(3, '0');
        }

        public int UpdateSanPham(IQueryable<DT_SanPham> data)
        {
            if (data != null)
            {
                foreach (DT_SanPham item in data)
                {
                    item.MaSanPham = GetNewMaSanPham(item.IDSanPham);
                    item.SoLuongTonKho = item.SoLuongTonKho == null ? item.SoLuongNhapThem : item.SoLuongTonKho + item.SoLuongNhapThem;
                    item.SoLuongNhapThem = 0;
                }
                return this.Context.SaveChanges();
            }
            return -1;
        }

        public IQueryable<DT_SanPham> GetData()
        {
            return this.Context.DT_SanPham.Where(s => !s.IDTrangThaiSanPham.HasValue || s.IDTrangThaiSanPham != (int?)StructsEnums.EnumTrangThaiSanPham.Huy);
        }
    }
}
